/**
 *@Description 收货地址配置
 *@Author chenxq
 *@Date 201516-01-08
 */
var receiverCtrl  = new XFApp.client();
XFApp.client.prototype.receiver = {
    successCode : "10000",
    page : null,
    urlItems : {
        queryByPageURL : basePath+"/logistics/receiver/queryPage",
        getReceiverURL : basePath+"/logistics/receiver/detail",
        saveReceiverURL : basePath+"/logistics/receiver/save",
        delReceiverURL : basePath+"/logistics/receiver/delete"
    },
    queryPage : function(){
        _this = this;
        if(!!_this.page){
            _this.page.page("remote",0,$('#searchForm').serialize());
        }else{
            _this.page = $("#page").page({
                pageSize : 10,
                currentPageIndex : Number($("#currentPageIndex").val())-1,
                remote: {
                    url: _this.urlItems.queryByPageURL,
                    params: $('#searchForm').serialize(),
                    success: function (data, pageIndex) {
                        $("#currentPageIndex").val(data.result.pageNo);
                        if(data.code==_this.successCode){
                            var pageResult = data.result;
                            if(null == data
                                || null == data.result
                                || null == data.result.itemList
                                || data.result.itemList.length < 1){
                                $("#page_grid").html('<tr><td align="center" colspan="11">暂无数据</td></tr>');
                            }else{
                                for(var index in pageResult.itemList){
                                    var element = pageResult.itemList[index];
                                    element["seqNum"]=parseInt(pageResult.pageSize)*(parseInt(pageResult.pageNo)-1)+parseInt(index)+1;
                                }
                                $("#page_grid").html($("#dataTmp").render(pageResult.itemList));
                            }
                        }else{
                            webDialogUtil.simpleAlert("数据查询失败!");
                        }
                    },
                    error : function(){
                        webDialogUtil.simpleAlert("数据查询失败!");
                    }
                },
                firstBtnText : '首页',
                lastBtnText : '尾页',
                prevBtnText : '上一页',
                nextBtnText : '下一页',
                showInfo : true,
                showJump : true,
                jumpBtnText : '跳转',
                showPageSizes : true,
                infoFormat : '共{pageCount}页,{total}项'
            });
        }
    },
    formValidate : function(){
        $("#editReceiverForm").validate({
            rules: {
                receiverName: "required",
                zipCode : {
                    number:true,
                    minlength:6,
                    maxlength:6
                },
                receiverPhone: {
                    required:true
                },
                concatEmail: {
                    required: true,
                    email:true
                },
                address: "required"
            },
            messages: {
                receiverName: "请填写收货人姓名",
                zipCode:  {
                    required: "请填写邮政编码",
                    number : "邮政编码格式错误",
                    minlength : "邮政编码长度错误",
                    maxlength : "邮政编码长度错误"
                },
                receiverPhone:  {
                    required : "请填写联系电话"
                },
                concatEmail: {
                    required : "请填写联系邮箱",
                    email : "邮箱格式错误"
                }
            }
        });
        $("#editReceiverForm").validate();
    },
    editReceiverView : function(id,typeId){
        _this = this;
        var logisticsId = $("#companyId").find("option:selected").val();
        var logisticsName = $("#companyId").find("option:selected").text();
        if(typeId == 2 && (logisticsId == null || logisticsId == '')){
            window.webDialogUtil.alert("提示","请选择物流公司");
            return;
        }
        if(null == id){
            return this.editReceiverDialog(null,typeId);
        }
        $.ajax({
            url:_this.urlItems.getReceiverURL,
            data:{id:id},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(false == data.isSuccess){
                    window.webDialogUtil.alert("提示","获取数据失败");
                    return;
                }
                _this.editReceiverDialog(data.result,typeId);
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    editReceiverDialog : function(data,typeId){
        if(null != data){
            $("#editReceiverForm #id").val(data.id);
            $("#editReceiverForm #logisticsId").val(data.logisticsId);
            $("#editReceiverForm #logisticsName").val(data.logisticsName);
            $("#editReceiverForm #receiverName").val(data.receiverName);
            var receiverArea = data.receiverArea.split(" ");
            var prov = receiverArea[0];
            var city = receiverArea[1];
            var dist = receiverArea[2];
            $("#area").citySelect({
                prov: prov,
                city: city,
                dist: dist
            });
            var isDefault = data.isDefault;
            if(isDefault == 1){
                $("#editReceiverForm #isDefault").attr("checked", true);
            }else{
                $("#editReceiverForm #isDefault").attr("checked", false);
            }
            $("#editReceiverForm #receiverArea").val(data.receiverArea);
            $("#editReceiverForm #receiverAddress").val(data.receiverAddress);
            $("#editReceiverForm #zipCode").val(data.zipCode);
            $("#editReceiverForm #receiverPhone").val(data.receiverPhone);
            $("#editReceiverForm #updateTime").text(data.updateTime);
            $("#editReceiverForm #updatePerson").text(data.updatePerson);
        }else{
            var logisticsId = $("#companyId").find("option:selected").val();
            var logisticsName = $("#companyId").find("option:selected").text();
            $("#editReceiverForm #id").val("");
            $("#editReceiverForm #logisticsId").val(logisticsId);
            $("#editReceiverForm #logisticsName").val(logisticsName);
            $("#editReceiverForm #receiverName").val("");
            $("#editReceiverForm #receiverArea").val("");
            $("#editReceiverForm #area").val("");
            $("#editReceiverForm #receiverAddress").val("");
            $("#editReceiverForm #zipCode").val("");
            $("#editReceiverForm #receiverPhone").val("");
            $("#editReceiverForm #isDefault").val("");
            $("#editReceiverForm #updateTime").text("");
            $("#editReceiverForm #updatePerson").text("");
        }
        webAlert({
            title:receiverCtrl.receiver.titleDisplay(typeId),
            content:document.getElementById("editView"),
            button:[
                {
                    name:"保存",
                    callback:function(){
                        var prov = $("#area .prov").find("option:selected").text();
                        var city = $("#area .city").find("option:selected").text();
                        var dist = $("#area .dist").find("option:selected").text();
                        var receiverArea = prov+ " " + city + " " + dist;
                        if(prov == '请选择' || city == '请选择'|| receiverArea == null){
                            $("#receiverArea-error").text("请选择收货区域");
                            return false;
                        }
                        $("#receiverArea").val(receiverArea);
                        if($("#editReceiverForm").validate().form()){
                            receiverCtrl.receiver.saveReceiverForm();
                        }else{
                            return false;
                        }
                    }},
                {
                    name:"关闭",
                    callback:function(){
                        this.close();
                    }
                }]
        });
    },
    saveReceiverForm : function(){
        _this = this;
        var isDefault = 0;
        if($("#isDefault").prop("checked")){
            isDefault = 1;
        }
        $.ajax( {
            url:_this.urlItems.saveReceiverURL,
            data:$('#editReceiverForm').serialize(),
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                window.webDialogUtil.alert("提示",data.msg);
                receiverCtrl.receiver.queryPage();
            },
            error : function() {
                window.webDialogUtil.alert("提示","操作失败");
            }
        });
    },
    titleDisplay : function(typeId){
        switch(typeId){
            case 0:return "查看";
            case 1:return "修改";
            case 2:return "新增";
            default : return "";
        }
    },
    delReceiverDialog : function(id){
        _this = this;
        webDialogUtil.simpleConfrimAlert("确定要删除此运单记录吗?", function(){receiverCtrl.receiver.delReceiver(id);});
    },
    delReceiver : function(id){
        _this = this;
        $.ajax({
            url:_this.urlItems.delReceiverURL,
            data:{id:id},
            type:'post',
            cache:false,
            dataType:'json',
            success:function(data) {
                if(true == data.isSuccess){
                    webDialogUtil.simpleAlert(data.msg);
                    receiverCtrl.receiver.queryPage();
                }
            },
            error : function() {
                webDialogUtil.simpleAlert("操作失败!");
            }
        });
    },
    event : function(){
        _this = this;
        $("#searchButton").on("click",function(){
            receiverCtrl.receiver.queryPage();
        });
        receiverCtrl.receiver.queryPage();
        this.formValidate();
    }
};
$.views.converters({

});
$(function(){
    receiverCtrl.receiver.event();
});